//
//  CSBPostDYSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/5.
//

import UIKit
import JDStatusBarNotification

class CSBPostDYSweeping: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    var csbColoter:UIColor = .brown
    var csbFplowe:CGFloat = 23.1
    var csbHeihei:UIFont?
    

    @IBOutlet weak var dymONeCSB: UIImageView!
    
    @IBOutlet weak var dymtwoCSB: UIImageView!
    
    @IBOutlet weak var mineHeadeCSB: UIImageView!
    
    
    @IBOutlet weak var sureAddPicCSB: UIButton!
    
    var minePickCSBVC:UIImagePickerController?
    
    @IBOutlet weak var minsendContentCSB: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        if csbColoter == UIColor.black {
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe)
            self.view.backgroundColor = csbColoter
        }else{
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe + 1)
        }
        guard csbHeihei != nil else { return  }

        if self.view.backgroundColor == csbColoter {
            let qurk = UIButton(configuration: UIButton.Configuration.plain())
            qurk.isHidden = true
            qurk.isEnabled = false
            view.addSubview(qurk)
        }
        let minPicker = UIImagePickerController()
        minPicker.delegate = self
        minPicker.allowsEditing = false
        minPicker.sourceType = .photoLibrary

        minePickCSBVC = minPicker
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

       
        mineHeadeCSB.image = CSBFetcherAll.Hammer.shoHeadCSB
        mineHeadeCSB.layer.cornerRadius = 22
        mineHeadeCSB.layer.masksToBounds = true
        mineHeadeCSB.contentMode = .scaleAspectFill
        minsendContentCSB.contentInset = UIEdgeInsets.init(top: 24, left: 45, bottom: 20, right: 12)
        if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
            minsendContentCSB.layer.cornerRadius = 20
            minsendContentCSB.layer.masksToBounds = true
        }
       
       
    }

    
    @IBAction func csbButtonClick(_ sender: UIButton) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.dismiss(animated: true)
    }
    
    
    @IBAction func sureCSBPOstTouc(_ sender: UIButton) {
      
        
        if minsendContentCSB.text == "" || minsendContentCSB.text == "Record the moment…" {
         
            NotificationPresenter.shared.present("Sorry,The post content cannot be empty!", includedStyle: .warning)
            return
        }
        if dymONeCSB.image == nil {

            NotificationPresenter.shared.present("At least one image is required for dynamics", includedStyle: .warning)
            return
        }
        
  
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: DispatchWorkItem(block: {
            var ikol = ["csbFplowe":self.csbFplowe]
            if self.view.backgroundColor == .brown {
                ikol["allElseTigerCSB"]  = 77.8
                self.csbFplowe += 1.1
            }else{
                self.csbFplowe += 2.1
            }

            if self.csbFplowe < 10{
                return
            }
            NotificationPresenter.shared.present("Upload successful, Please be patient and wait for the review....", includedStyle: .success)
            self.dismiss(animated: true)
            return
        }))
        

        
    }
    
    @IBAction func inssertNewPicToClick(_ sender: UIButton) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
    
            self.present(self.minePickCSBVC!, animated: true, completion: nil)
        }else {
            NotificationPresenter.shared.present("There is no album permission", includedStyle: .warning)
        }
        
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        let image : UIImage? = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
       
        DispatchQueue.main.async {
            var csbBayui = [Int]()
            let csbNUmber = Array(repeating: 10, count: 64)
            let csbNumbe2 = Array(repeating: 96, count: 50)
            var cabSunmAll: Int = 0

            csbBayui.append(11)
            cabSunmAll = csbNUmber.count
            if cabSunmAll > 5 {
                csbBayui.append(contentsOf: csbNUmber)
            }else{
                csbBayui.append(contentsOf: csbNumbe2)
            }

           

            self.minePickCSBVC?.dismiss(animated: true)
            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

           
            if self.dymONeCSB.image == nil{
                self.dymONeCSB.image = image
                if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
                    self.sureAddPicCSB.snp.remakeConstraints { make in
                        make.edges.equalTo(self.dymtwoCSB)
                    }
                }
               
            }else{
                self.dymtwoCSB.image = image
                self.sureAddPicCSB.isHidden = true
            }
           
        }
        
    }
}
